const {
  getBasicSalary,
  addBasicSalary,
  editBasicSalary,
  deleteBasicSalary,
  getWorkTypeSalary,
  addWorkTypeSalary,
  editWorkTypeSalary,
  deleteWorkTypeSalary
} = require('../service/salary.service');
const { judgeIfMissParam } = require('../utils/common')

class SalaryController {
  // 获取基本工资数据
  async getBasicSalary(ctx, next) {
    const result = await getBasicSalary();
    ctx.body = {
      ret: 0,
      data: result
    }
  }

  // 添加基本工资数据
  async addBasicSalary(ctx, next) {
    try {
      const requireArr = ['start', 'end', 'baseSalary', 'mealAllowance', 'temperatureAllowance',
      'monthCheck', 'seasonCheck', 'scoreOne', 'scoreTwo', 'scoreThree', 'scoreFour'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_addBasicSalary]', requireArr, ctx.request.body);
      if (!flag) {
        const error = new Error('1004 [salary.controller_addBasicSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await addBasicSalary(ctx.request.body);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_addBasicSalary]', e);
    }
  }

  // 修改基本工资数据
  async editBasicSalary(ctx, next) {
    try {
      const requireArr = ['id', 'start', 'end', 'baseSalary', 'mealAllowance', 'temperatureAllowance',
      'monthCheck', 'seasonCheck', 'scoreOne', 'scoreTwo', 'scoreThree', 'scoreFour'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_editBasicSalary]', requireArr, ctx.request.body);
      if (!flag) {
        const error = new Error('1004 [salary.controller_editBasicSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await editBasicSalary(ctx.request.body);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_editBasicSalary]', e);
    }
  }

  // 删除基本工资数据
  async deleteBasicSalary(ctx, next) {
    try {
      const requireArr = ['id'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_deleteBasicSalary]', requireArr, ctx.query);
      if (!flag) {
        const error = new Error('1004 [salary.controller_deleteBasicSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await deleteBasicSalary(ctx.query);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_deleteBasicSalary]', e);
    }
  }

  // 获取岗位工资数据
  async getWorkTypeSalary(ctx, next) {
    try {
      const result = await getWorkTypeSalary();
      ctx.body = {
        ret: 0,
        data: result
      }
    } catch(e) {
      console.log('[salary.controller_getWorkTypeSalary]', e);
    }
  }

  // 添加岗位工资数据
  async addWorkTypeSalary(ctx, next) {
    try {
      const requireArr = ['work', 'workSalary'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_addWorkTypeSalary]', requireArr, ctx.request.body);
      if (!flag) {
        const error = new Error('1004 [salary.controller_deleteBasicSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await addWorkTypeSalary(ctx.request.body);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_addWorkTypeSalary]', e);
    }
  }

  // 编辑岗位工资数据
  async editWorkTypeSalary(ctx, next) {
    try {
      const requireArr = ['id', 'work', 'workSalary'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_editWorkTypeSalary]', requireArr, ctx.request.body);
      if (!flag) {
        const error = new Error('1004 [salary.controller_editWorkTypeSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await editWorkTypeSalary(ctx.request.body);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_editWorkTypeSalary]', e);
    }
  }

  // 删除岗位工资数据
  async deleteWorkTypeSalary(ctx, next) {
    try {
      const requireArr = ['id'];
      const flag = judgeIfMissParam(ctx, '[salary.controller_deleteWorkTypeSalary]', requireArr, ctx.query);
      if (!flag) {
        const error = new Error('1004 [salary.controller_deleteWorkTypeSalary]');
        return ctx.app.emit('error', error, ctx);
      }

      const result = await deleteWorkTypeSalary(ctx.query);
      if (!result) return;
      ctx.body = {
        ret: 0,
        data: 'ok'
      }
    } catch(e) {
      console.log('[salary.controller_deleteWorkTypeSalary]', e);
    }
  }
}

module.exports = new SalaryController();